const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    cartNum:0,
    detail: {},
    banners: [],
    descPics: []
  },
  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    let id = options.id;
    this.getProductDetail(id);
    this.setData({
      id
    })
  },

  toCart() {
    wx.navigateTo({
      url: 'pages/cart/cart',
    })
  },

  addCart(event) {
    let url = '/cart/add';
    let data = {product_id: this.data.id,buyNum:1};
    app.post(url,data).then(res=> {
      console.log(res);
      let cartNum = this.data.cartNum;
      this.setData({
        cartNum: ++cartNum
      })
    }).catch(err=> {
      console.log(err);
    })
  }, 

  getProductDetail(id) {
    let url = "/product/detail";
    app
      .get(url, { id: id })
      .then(res => { 
        this.setData({
          cartNum: res.data.cartNum,
          detail: res.data,
          banners: res.data.banners,
          descPics: res.data.descPics
        });
      })
      .catch(err => {
        console.log(err);
      });
  }
});
